home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992…ugust: Hack to the Future / ADC Developer CD (1992-08) (''Hack To The Future'')_iso / Dev.CD 199208.iso / Technical Documentation / DTS Sample Code / Snippets / Toolbox & IAC / AppleEvents / ODOC / odoc.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  2.6 KB  |  116 lines  |  [TEXT/KAHL]

  1. /*    open document snippet
  2.     Steven Falkenburg 10/2/91
  3.     
  4.     This sample sends an 'odoc' event to a specified running application
  5. */
  6.  
  7. #include <PPCToolBox.h>
  8. #include <AppleEvents.h>
  9. #include <Aliases.h>
  10.  
  11. void main(void);
  12. void InitStuff(void);
  13. OSErr GetTargetAddress(Str255 prompt,Str255 appStr,PortInfoRec *myPortInfo,
  14.                         AEAddressDesc *targetAddress,TargetID *toTargetID);
  15. OSErr SendODOC(AEAddressDesc *targetAddress,AliasHandle targetFile);
  16. Boolean GetFileToOpen(FSSpec *target);
  17.  
  18. void main(void)
  19. {
  20.     PortInfoRec portInfo;
  21.     AEAddressDesc targetAddress;
  22.     TargetID targetID;
  23.     OSErr err;
  24.     FSSpec fileSpec;
  25.     AliasHandle aliasTarget;
  26.     
  27.     InitStuff();
  28.     
  29.     if (!GetFileToOpen(&fileSpec))
  30.         ExitToShell();
  31.     
  32.     if (GetTargetAddress("\pSpecify target of ODOC:",nil,&portInfo,&targetAddress,&targetID)==noErr) {
  33.         err = NewAlias(nil,&fileSpec,&aliasTarget);
  34.         if (err==noErr) {
  35.             err = SendODOC(&targetAddress,aliasTarget);
  36.             err = AEDisposeDesc(&targetAddress);
  37.             DisposHandle(aliasTarget);
  38.         }
  39.     }
  40. }
  41.  
  42.  
  43. void InitStuff(void)
  44. {
  45.     InitGraf(&qd.thePort);
  46.     InitFonts();
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs(nil);
  51.     InitCursor();
  52.     FlushEvents(everyEvent,0);
  53. }
  54.  
  55.  
  56. Boolean GetFileToOpen(FSSpec *target)
  57. {
  58.     StandardFileReply sfReply;
  59.     
  60.     StandardGetFile(nil,-1,nil,&sfReply);
  61.     BlockMove(&sfReply.sfFile,target,sizeof(FSSpec));
  62.     
  63.     return sfReply.sfGood;
  64. }
  65.  
  66.  
  67. OSErr GetTargetAddress(Str255 prompt,Str255 appStr,PortInfoRec *myPortInfo,
  68.                         AEAddressDesc *targetAddress,TargetID *toTargetID)
  69. {
  70.     OSErr err;
  71.     
  72.     err = PPCBrowser(prompt,appStr,false,&toTargetID->location,myPortInfo,nil,"\p");
  73.     if (err==noErr) {
  74.         BlockMove(&myPortInfo->name,&toTargetID->name,256);
  75.         err = AECreateDesc(typeTargetID,(Ptr)toTargetID,sizeof(TargetID),targetAddress);
  76.     }
  77.     
  78.     return err;
  79. }
  80.  
  81.  
  82. OSErr SendODOC(AEAddressDesc *targetAddress,AliasHandle targetFile)
  83. {
  84.     OSErr err;
  85.     AppleEvent theAE,reply;
  86.     AEDescList descList;
  87.     
  88.     reply.dataHandle = nil;
  89.     
  90.     err = AECreateAppleEvent(kCoreEventClass,kAEOpenDocuments,targetAddress,
  91.                 kAutoGenerateReturnID,kAnyTransactionID,&theAE);
  92.     if (err!=noErr)
  93.         return err;
  94.     
  95.     err = AECreateList(nil,0,false,&descList);
  96.     if (err!=noErr)
  97.         return err;
  98.     
  99.     HLock(targetFile);
  100.     err = AEPutPtr(&descList,0,typeAlias,(Ptr)*targetFile,sizeof(AliasHandle)+(**targetFile).aliasSize);
  101.     HUnlock(targetFile);
  102.     if (err!=noErr)
  103.         return err;
  104.     
  105.     err = AEPutParamDesc(&theAE,keyDirectObject,&descList);
  106.     if (err!=noErr)
  107.         return err;
  108.     
  109.     err = AESend(&theAE,&reply,kAENoReply,kAENormalPriority,kAEDefaultTimeout,nil,nil);
  110.     
  111.     err = AEDisposeDesc(&descList);
  112.     err = AEDisposeDesc(&theAE);
  113.     if (reply.dataHandle)
  114.         err = AEDisposeDesc(&reply);
  115. }
  116.